<?php

namespace api\controller;

use common\exception\LogicException;
use common\service\MotionCaptureDataService;
use common\validate\MotionCaptureData;
use support\ErrorCode;
use think\Controller;
use think\Request;


class MotionCaptureDataController extends Controller
{
    /**
     * @var \common\service\MediaService
     */
    protected $motionCaptureDataService;

    public function __construct()
    {
        parent::__construct();
        $this->motionCaptureDataService = MotionCaptureDataService::getInstance();
    }

    /**
     * 保存动捕数据
     * @param \think\Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function saveMotionCaptureData(Request $request)
    {
        $data = $request->param()['data'];
        $validate = new MotionCaptureData();
        if (!empty($data['motion_capture_data_relation'])) {
            $validate->scene('SaveMotionCaptureDataRelation');
            foreach ($data["motion_capture_data_relation"] as $motionCaptureDataRelationItem) {
                if (!$validate->check($motionCaptureDataRelationItem)) {
                    throw_strack_exception($validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
                }
            }
        }

        try {
            $resData = $this->motionCaptureDataService->saveMotionCaptureData($data, $request->getUserId());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取动捕数据列表
     * @param \think\Request $request
     * @return \think\Response
     */
    public function getMotionCaptureDataList(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->motionCaptureDataService->getMotionCaptureDataList($param);
        return json(success_response('', $resData));
    }

    /**
     * 移除动捕数据
     * @param \think\Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function removeMotionCaptureData(Request $request)
    {
        $filter = $request->param()['param']['filter'];
        $resData = $this->motionCaptureDataService->removeMotionCaptureData($filter);
        return json(success_response('', $resData));
    }

    /**
     * 发布动捕任务
     * @throws \Throwable
     * @throws \think\Exception
     */
    public function publishMotionCaptureTask(Request $request)
    {
        $data = $request->param()['data'];
        try {
            $resData = $this->motionCaptureDataService->publishMotionCaptureTask($data);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

}
